package cn.idietrade.idietradeservicemodule8001.controller;

import cn.idietrade.common.pojo.SecondService;
import cn.idietrade.common.pojo.SecondUser;
import cn.idietrade.common.utils.EmptyUtils;
import cn.idietrade.common.utils.ReturnResultData;
import cn.idietrade.idietradeservicemodule8001.service.SecondServiceService;
import cn.idietrade.idietradeservicemodule8001.utils.RedisUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author zjy
 * @version 1.0
 * @createTime 2022-11-29 17:29
 * @description
 */
@Slf4j
@RestController
public class SecondServiceController {
    @Resource
    private RedisUtil redisUtil;

    @Resource
    private SecondServiceService secondServiceService;

    Logger logger = Logger.getLogger("SecondServiceController");

    //申诉列表查询
    @RequestMapping("/queryChattingRecordsAll")
    public ReturnResultData<PageInfo<SecondService>> queryChattingRecordsAll(@RequestParam(value = "token",required = false) String token, @RequestParam(value = "pageNum",required = false)Integer pageNum,@RequestParam(value = "pageSize",required = false) Integer pageSize,@RequestParam(value = "selectId",required = false) String selectId){
        ReturnResultData<PageInfo<SecondService>> resultData =new ReturnResultData<>();
            try {
                SecondUser secondUser =null;
                if(EmptyUtils.isNotEmpty(token)){
                    secondUser = JSON.parseObject(redisUtil.getStr(token), SecondUser.class);
                }
                int userId=0;
                if(EmptyUtils.isNotEmpty(selectId)){
                    userId=secondUser.getId();
                }
                PageInfo<SecondService> secondServices = secondServiceService.queryChattingRecordsAll(pageNum,pageSize,userId);
                if(EmptyUtils.isNotEmpty(secondServices)){
                    resultData.setCode(1);
                    resultData.setMessage("申诉列表查询成功");
                    resultData.setData(secondServices);
                }else{
                    resultData.setCode(0);
                    resultData.setMessage("暂无申诉记录");
                    resultData.setData(null);
                }
            }catch (Exception e){
                resultData.setData(null);
                resultData.setCode(-1);
                resultData.setMessage("程序异常");
                logger.info("申诉列表查询----程序异常");
                logger.info(e.getMessage());
                e.printStackTrace();
            }
        return resultData;
    }


    //保存用户申诉内容
    @RequestMapping("/saveChattingRecords")
    public ReturnResultData saveChattingRecords(String token,SecondService secondService){
        ReturnResultData resultData =new ReturnResultData<>();
            try {
                SecondUser secondUser = JSON.parseObject(redisUtil.getStr(token), SecondUser.class);
                secondService.setUserId(secondUser.getId());
                int i = secondServiceService.saveChattingRecords(secondService);
                if(i>0){
                    resultData.setCode(1);
                    resultData.setMessage("用户申诉内容保存成功");
                    resultData.setData(null);
                }else{
                    resultData.setCode(0);
                    resultData.setMessage("用户申诉内容保存失败");
                    resultData.setData(null);
                }
            }catch (Exception e){
                resultData.setData(null);
                resultData.setCode(-1);
                resultData.setMessage("程序异常");
                logger.info("用户申诉内容保存----程序异常");
                logger.info(e.getMessage());
                e.printStackTrace();
            }
        return resultData;
    }


    //修改服务进度
    @RequestMapping("/updateServiceState")
    public ReturnResultData updateServiceState(Integer serviceId){
        ReturnResultData resultData =new ReturnResultData<>();
            try {
                int i = secondServiceService.updateServiceState(serviceId);
                if(i>0){
                    resultData.setCode(1);
                    resultData.setMessage("服务状态修改成功");
                    resultData.setData(null);
                }else{
                    resultData.setCode(0);
                    resultData.setMessage("服务状态修改失败");
                    resultData.setData(null);
                }
            }catch (Exception e){
                resultData.setData(null);
                resultData.setCode(-1);
                resultData.setMessage("程序异常");
                logger.info("服务状态修改----程序异常");
                logger.info(e.getMessage());
                e.printStackTrace();
            }
        return resultData;
    }

}
